cmake_minimum_required(VERSION 3.15)
project(cpp_backend VERSION 2.0.0 LANGUAGES CXX)

# ========================================
# C++20标准设置（微服务架构要求）
# ========================================
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# 设置输出路径
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

# 编译选项
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0 -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -DNDEBUG")

# ========================================
# 查找依赖库
# ========================================

# Boost
find_package(Boost 1.71 REQUIRED COMPONENTS system thread filesystem)

# Protobuf
find_package(Protobuf REQUIRED)

# gRPC（微服务架构新增）
find_library(GRPC_LIB grpc++ REQUIRED)
find_library(GRPC_REFLECTION_LIB grpc++_reflection REQUIRED)
find_program(GRPC_CPP_PLUGIN grpc_cpp_plugin REQUIRED)

# 创建gRPC imported targets（兼容我们的代码）
if(NOT TARGET gRPC::grpc++)
    add_library(gRPC::grpc++ UNKNOWN IMPORTED)
    set_target_properties(gRPC::grpc++ PROPERTIES
        IMPORTED_LOCATION "${GRPC_LIB}")
endif()

if(NOT TARGET gRPC::grpc++_reflection)
    add_library(gRPC::grpc++_reflection UNKNOWN IMPORTED)
    set_target_properties(gRPC::grpc++_reflection PROPERTIES
        IMPORTED_LOCATION "${GRPC_REFLECTION_LIB}")
endif()

if(NOT TARGET gRPC::grpc_cpp_plugin)
    add_executable(gRPC::grpc_cpp_plugin IMPORTED)
    set_target_properties(gRPC::grpc_cpp_plugin PROPERTIES
        IMPORTED_LOCATION "${GRPC_CPP_PLUGIN}")
endif()

# nlohmann_json
find_package(nlohmann_json 3.2.0 QUIET)
if(NOT nlohmann_json_FOUND)
    message(STATUS "nlohmann_json not found via find_package, trying header-only mode")
endif()

# MySQL Connector C++
find_library(MYSQLCPPCONN_LIB mysqlcppconn REQUIRED)

# Hiredis
find_library(HIREDIS_LIB hiredis REQUIRED)

# ========================================
# 包含目录
# ========================================
include_directories(
    ${CMAKE_SOURCE_DIR}
    ${CMAKE_SOURCE_DIR}/common_component/include
    ${CMAKE_SOURCE_DIR}/logger
    ${Boost_INCLUDE_DIRS}
    ${PROTOBUF_INCLUDE_DIRS}
)

# ========================================
# 子目录（按依赖顺序）
# ========================================

# 1. gRPC协议（最先编译，其他模块依赖）
add_subdirectory(protos)

# 2. 基础组件
add_subdirectory(logger)
add_subdirectory(common_component)
add_subdirectory(config)
add_subdirectory(database)

# 3. 框架层（包含微服务组件）
add_subdirectory(framework)

# 4. 网关层（Gateway V3 - 微服务架构）
if(EXISTS ${CMAKE_SOURCE_DIR}/gateway/v3/CMakeLists.txt)
    add_subdirectory(gateway/v3)
else()
    message(WARNING "gateway/v3 not found, skipping legacy gateway build")
endif()

# 新增：基于Boost.Beast的精简HTTP网关
add_subdirectory(gateway/beast_http)

# 5. 业务服务层
add_subdirectory(services)

# 6. 测试
add_subdirectory(test)

# ========================================
# 主程序（如果需要单进程版本）
# ========================================
# add_executable(cpp_backend_server
#     bak/main.cpp
# )
# 
# target_link_libraries(cpp_backend_server
#     logger
#     common_component
#     pthread
# )

# ========================================
# 测试
# ========================================
enable_testing()

# ========================================
# 安装规则
# ========================================
# install(TARGETS cpp_backend_server DESTINATION bin)
install(DIRECTORY ${CMAKE_SOURCE_DIR}/config/ DESTINATION etc/cpp_backend)

message(STATUS "===========================================")
message(STATUS "  cpp_backend v${PROJECT_VERSION}")
message(STATUS "  C++ Standard: C++${CMAKE_CXX_STANDARD}")
message(STATUS "  Build Type: ${CMAKE_BUILD_TYPE}")
message(STATUS "  Microservice Architecture: ENABLED")
message(STATUS "===========================================")
